package org.sgx.gwteditors.client.impl1.misc;

import java.util.List;

import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.error.EditorError;
import org.sgx.gwteditors.client.editor.error.EditorErrorManager;
import org.sgx.gwteditors.client.editor.event.EditorEventManager;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.EditorManager1;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * an editor password strings. 
 * 
 * @author sg
 * 
 */
public class PasswordEditor extends PasswordTextBox implements Editor<String>, ValueChangeHandler<String> {

	@Override
	public boolean isSimple() {
		return true;
	}

	public PasswordEditor() {
		super();
		addValueChangeHandler(this);
	}

	@Override
	public String flush() {
		return getText();
		// no validation / errors
	}

	@Override
	public void load(String t) {
		setText(t);
	}

	@Override
	public Widget getWidget() {
		return this;
	}

	@Override
	public void addValueChangeListener(ValueChangeListener<String> l) {
		EditorEventManager.<String> getInstance().addValueChangeListener(this, l);
		// image.setUrl(textBox.getText());
	}

	@Override
	public void removeValueChangeListener(ValueChangeListener<String> l) {
		EditorEventManager.<String> getInstance().removeValueChangeListener(this, l);
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		EditorEventManager.<String> getInstance().fireValueChangeGwtEvent(this, event);
	}

	@Override
	public List<EditorError<String>> getErrors() {
		return EditorErrorManager.<String> getInstance().getErrors(this);
	}

	@Override
	public String getEditorId() {
		return EditorManager1.getInstance().getEditorName(this);
	}

	@Override
	public String[] getEditorTypesSupported() {
		return new String[] { EditorFramework1.TYPE_STRING };
	}
	// public boolean isEditable() {
	// return editable;
	// }
	// public void setEditable(boolean editable) {
	// this.editable = editable;
	// }
	// @Override
	// public void config(String configStr) {
	// JsObject jso = JsonUtils.unsafeEval("{"+configStr+"}");
	// if(jso.objGetBoolean("editable"))
	// editable=true;
	// else
	// editable=false;
	// if(jso.objGetString("width")!=null)
	// image.setWidth(jso.objGetString("width"));
	// if(jso.objGetString("height")!=null)
	// image.setHeight(jso.objGetString("height"));
	// }

}
